package com.coolwen.spring.aop.java;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;

/**
 * @author CoolWen
 * @version 2020-03-26 8:48
 */
//@Component
//@Aspect
public class PerformAspect {


    //使用before的advise
    @Before("execution(* com.coolwen.spring.aop.Annotation.CalFibonacciImpl.*(..))")
    public void testBefore() {
        System.out.println("run before");
    }

    //使用after的advise
    @After("execution(* com.coolwen.spring.aop.Annotation.CalFibonacciImpl.*(..))")
    public void testAfter() {
        System.out.println("run after");
    }

    //效率统计，使用环绕
    @Around("execution(* com.coolwen.spring.aop.Annotation.CalFibonacciImpl.*(..))")
    public long statPerform(ProceedingJoinPoint jp) {
        long result = 0;
        try {
            long start = System.currentTimeMillis();
            result = (long) jp.proceed();
            long end = System.currentTimeMillis();
            System.out.println(jp.getSignature().getName() + "花费了:" + (end - start));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }


}
